using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace TidhiGanitam.DataModel
{
    public class AmrutaGhadiyaluRoutineItem
    {
        private int m_Year;
        DateTime m_Date;
        private int m_SNo;
        NakshatramuRoutineItem m_NakshatramuDetails;
        DinamuRoutineItem m_DinamuRoutines;
        ArrayList m_PrAmrutaGhadiyaluDetails;
        ArrayList m_AmrutaGhadiyaluDetails;

        public int Bhagam
        {
            get { return m_SNo; }
        }

        public DateTime Date
        {
            get { return m_Date; }
        }

        public ArrayList PreviousAmrutaGhadiyaluDetails
        {
            get
            { return m_PrAmrutaGhadiyaluDetails; }
        }
        public ArrayList AmrutaGhadiyaluDetails
        {
            get
            { return m_AmrutaGhadiyaluDetails; }
        }


        public AmrutaGhadiyaluRoutineItem(int SNo, int Year, DateTime Date, NakshatramuRoutineItem NakshatramuDetails,
            DinamuRoutineItem CurrentDinamuRoutine)
        {
            m_SNo = SNo;
            m_Year = Year;
            m_Date = Date;
            m_NakshatramuDetails = NakshatramuDetails;
            m_DinamuRoutines = CurrentDinamuRoutine;
            m_PrAmrutaGhadiyaluDetails = new ArrayList();
            m_AmrutaGhadiyaluDetails = new ArrayList();
        }
        public void Calculate(DinamuRoutineItem PreviousDinamuRoutine, NakshatramuRoutineItem PreviousNakshatramuRoutine,
            AmrutaGhadiyaluRoutineItem PreviousAmrutaGhadiyaluRoutine)
        {
            int NakshatramuIndex = -1;
            foreach (NakshatramuDetails Nakshatramu in m_NakshatramuDetails.NakshatramuDetails)
            {
                if (Nakshatramu.NakshatramuState == Enums.TidhiState.Purti)
                {
                    continue;
                }
                double dMultiplier = (double)Constants.AmrutaGhadiyalu[Convert.ToInt32(Nakshatramu.NakshatramuIndex)];
                Rasi AmruGhadi_Value = new Rasi(Nakshatramu.TotalSpan * dMultiplier);

                //We need to find out previous Nakshatramu time.
                Rasi NakshatramPreviousValue = new Rasi();
                if (NakshatramuIndex > -1)
                {
                    NakshatramPreviousValue = new Rasi(((NakshatramuDetails)m_NakshatramuDetails.NakshatramuDetails[NakshatramuIndex]).EndTime);
                }
                else
                {
                    foreach (NakshatramuDetails nak in PreviousNakshatramuRoutine.NakshatramuDetails)
                    {
                        NakshatramPreviousValue += nak.EndTime;
                    }
                }
                if (PreviousNakshatramuRoutine.PreviousNakshatramuState == Enums.TidhiState.Purti)
                {
                    Rasi TwoDaysBackValue = Nakshatramu.TotalSpan - Nakshatramu.EndTime - PreviousNakshatramuRoutine.NakshatramuTime;
                    AmruGhadi_Value = AmruGhadi_Value - TwoDaysBackValue;
                    NakshatramPreviousValue = new Rasi();
                }
                //Find Total AmrutaGhadiyalu
                Rasi TotalAmrutaGhadiyaluTime = AmruGhadi_Value + NakshatramPreviousValue;
                if (Nakshatramu.NakshatramuState == Enums.TidhiState.Eshyam)
                {
                    AmrutaGhadiyaluDetails AmrutaGhadiyaluItem = new AmrutaGhadiyaluDetails();
                    AmrutaGhadiyaluItem.NakshatramuIndex = Nakshatramu.NakshatramuIndex;
                    AmrutaGhadiyaluItem.AmrutaGhadiyaluTime = new Rasi(TotalAmrutaGhadiyaluTime);
                    m_AmrutaGhadiyaluDetails.Add(AmrutaGhadiyaluItem);
                }
                else if (TotalAmrutaGhadiyaluTime > PreviousDinamuRoutine.RojuPramanam)
                {
                    //Scenario 1
                    //Subtract Previous Dina Pramanamu from this
                    Rasi AmrutaGhadiyaluTime = TotalAmrutaGhadiyaluTime - PreviousDinamuRoutine.RojuPramanam;
                    AmrutaGhadiyaluDetails AmrutaGhadiyaluItem = new AmrutaGhadiyaluDetails();
                    AmrutaGhadiyaluItem.NakshatramuIndex = Nakshatramu.NakshatramuIndex;
                    AmrutaGhadiyaluItem.AmrutaGhadiyaluTime = new Rasi(AmrutaGhadiyaluTime);
                    m_AmrutaGhadiyaluDetails.Add(AmrutaGhadiyaluItem);
                }
                else
                {
                    //Scenario 2
                    //If Total AmrutaGhadiyalu time is less than previous roju pramanamu then
                    //AmrutaGhadiyalu goes to previous day
                    AmrutaGhadiyaluDetails PrAmrutaGhadiyaluItem = new AmrutaGhadiyaluDetails();
                    PrAmrutaGhadiyaluItem.NakshatramuIndex = Nakshatramu.NakshatramuIndex;
                    PrAmrutaGhadiyaluItem.AmrutaGhadiyaluTime = new Rasi(TotalAmrutaGhadiyaluTime);
                    m_PrAmrutaGhadiyaluDetails.Add(PrAmrutaGhadiyaluItem);
                    //Add this item to previous day's AmrutaGhadiyalu details
                    PreviousAmrutaGhadiyaluRoutine.AmrutaGhadiyaluDetails.Add(PrAmrutaGhadiyaluItem);
                }
                NakshatramuIndex++;
            }
        }
    }
}
